package com.yc.sign;

import com.yc.sign.web.LoginInterceptor;
import com.yc.sign.web.bean.SignProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.sql.DataSource;

/**
 * Company 源辰信息
 *
 * @author navy
 * @date 2024/8/25
 * Email haijunzhou@hnit.edu.cn
 */
@AutoConfiguration
@ComponentScan("com.yc.sign")
@ConditionalOnProperty(prefix = "sign", name = "enable", havingValue = "true")
public class SignConfig implements WebMvcConfigurer {
    // 如何访问数据库: MyBatis or Jdbc

    @Bean
    @ConditionalOnMissingBean(JdbcTemplate.class)
    JdbcTemplate jdbcTemplate(DataSource dataSource){
        return new JdbcTemplate(dataSource);
    }

    @Autowired
    LoginInterceptor loginInterceptor;
    @Autowired
    SignProperties properties;
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(loginInterceptor)
                .addPathPatterns(properties.getUrls());
    }
}
